﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateListNewsByGroup : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UpdateListNewsByGroup";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật tin theo nhóm");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateListNewsByGroup));
            string ListNewsByGroupId = System.Web.HttpContext.Current.Request["ListNewsByGroupId"];
            ListNewsByGroupCls ListNewsByGroupCls = new ListNewsByGroupBLL().CreateModel(Environment.Environments.ActiveLanguage, ListNewsByGroupId);
            if (ListNewsByGroupCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }


            string SelectLanguageText =
           "<select id=divSelectLanguage onchange=javascript:DetectUpdateListNewsByGroupBody('" + ListNewsByGroupId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "ListNewsByGroup.js") +
                //Portal.Environment.WebPartUtilities.AttachStyleSheet("ListNewsByGroup", "StyleSheet.css") +
                //"<div class=cssForm>" +
                //"<div class=cssFormTitle>" + ResourceBLL.GetText(WebPartId,"UpdateListNewsByGroupFormTitle","Cập nhật tin theo nhóm") + "</div>" +
                //"<div class=cssFormContent>" +
                             Environment.Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateListNewsByGroupFormLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateListNewsByGroupCaption", "Tin theo nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtListNewsByGroup value=\"" + ListNewsByGroupCls.Subject+ "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Url", "Urltheo nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtUrl value=\"" + ListNewsByGroupCls.JumpGroupUrl + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateListNewsByGroup('" + ListNewsByGroupId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateListNewsByGroupSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateListNewsByGroupCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "UpdateListNewsByGroupFormTitle", "Cập nhật tin theo nhóm"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateListNewsByGroup(
            string ActiveLanguage,
            string ListNewsByGroupId,
            string ListNewsByGroupName,
            string JumpGroupUrl)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (ListNewsByGroupName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên"));
                new ListNewsByGroupBLL().UpdateListNewsByGroup(null, ActiveLanguage, ListNewsByGroupId, ListNewsByGroupName, JumpGroupUrl);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetSubject(string ActiveLanguage, string ListNewsByGroupId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                ListNewsByGroupCls ListNewsByGroupCls = new ListNewsByGroupBLL().CreateModel(ActiveLanguage, ListNewsByGroupId);
                return ListNewsByGroupCls.Subject;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}